﻿using AutoMapper;
using MediatR;
using MyRabbit.Application.Command.ImgCommand;
using MyRabbit.Application.ErrorCode;
using MyRabbit.Domain;
using MyRabbit.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace MyRabbit.Application.CommandHandler.ImgCommandHandler
{
    public class ImgAddCommandHandler : IRequestHandler<ImgAddCommand, APIResult<object>>
    {
        /// <summary>
        /// 图片仓储
        /// </summary>
        private readonly IRepository<ImgInfo> ImgInfoRep;
        /// <summary>
        /// 图片中间仓储
        /// </summary>
        private readonly IRepository<ImgInfos> ImgInfosRep;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="ImgInfoRep"></param>
        /// <param name="ImgInfosRep"></param>
        /// <param name="mapper"></param>
        public ImgAddCommandHandler(IRepository<ImgInfo> ImgInfoRep, IRepository<ImgInfos> ImgInfosRep, IMapper mapper)
        {
            this.ImgInfoRep = ImgInfoRep;
            this.ImgInfosRep = ImgInfosRep;
            this.mapper = mapper;

        }



        /// <summary>
        /// 处理添加命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public async Task<APIResult<object>> Handle(ImgAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.Code = Codes.添加成功;
            result.Message = Codes.添加成功.ToString();


            using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                try
                {
                    ImgInfo images = new ImgInfo();

                    images.ImgName = request.fileUrl;

                    await ImgInfoRep.InsertAsync(images);

                    ImgInfos imginfos = new ImgInfos();

                    imginfos.ImgId = images.ImgId;
                    switch (request.isNo)
                    {
                        //产品表
                        case 1:
                            imginfos.TheRestId = request.TheRestId;
                            break;
                        //免疫
                        case 2:
                            imginfos.ImmunityId = request.TheRestId;
                            break;
                        //养殖
                        case 3:
                            imginfos.BreedId = request.TheRestId;
                            break;
                        //养殖企业
                        case 4:
                            imginfos.FarmEnterpriseId = request.TheRestId;
                            break;
                        //屠宰
                        case 5:
                            imginfos.SlaughteId = request.TheRestId;
                            break;
                        //屠宰企业
                        case 6:
                            imginfos.SlaughteEnterpriseId = request.TheRestId;
                            break;
                        //深加工
                        case 7:
                            imginfos.MachiningId = request.TheRestId;
                            break;
                        //深加工企业
                        case 8:
                            imginfos.MachiningEnterpriseId = request.TheRestId;
                            break;
                        //原料
                        case 9:
                            imginfos.MaterialId = request.TheRestId;
                            break;
                        //辅料
                        case 10:
                            imginfos.AccessoriesId = request.TheRestId;
                            break;
                        //添加剂
                        case 11:
                            imginfos.AdditiveId = request.TheRestId;
                            break;
                        //包材
                        case 12:
                            imginfos.PackeMaterialId = request.TheRestId;
                            break;

                        default:
                            // 可以根据需要处理其他情况
                            break;
                    }

                    await ImgInfosRep.InsertAsync(imginfos);

                    result.Data = images.ImgId;

                    //提交
                    scope.Complete();
                }
                catch (Exception ex)
                {
                    result.Code = Codes.添加失败;
                    result.Message = Codes.添加失败 + ex.Message;
                }
            }
            return await Task.FromResult(result);
        }
    }
}
